var ClubGroup_Hequnshuju = Item.extend({
    _ccsFileName: res.ClubGroup_Hequnshuju_json,
    _clubId: null,
    _tableViewList: [],

    _selectPos: 0,
    _selectedMapIdStr: '',
    _selectGroupPlayers: null,
    _selectStartData: null,   //选择的查询起始时间
    _selectEndData: null,    //选择的查询结束时间
    _scoreArea: '',

    _startYear: null,  //开始年
    _startMonth: null, //开始月
    _startDete: null,  //开始日

    _endYear: null,  //结束年
    _endMonth: null, //结束月
    _endDete: null,  //结束日

    _currentSelect: 0,
    _currentselectYear: null,    //日期选择的 年
    _currentselectMonth: null,   //日期选择的 月
    _currentselectday: null,     //日期选择的 日

    _memberData:[],

    _clubId: null,

    uid2ud:{},

    _curWanfa: '',
    _btn2wanfa:{
        0: '',
        1: '101',
        2: '301',
        3: '302'
    },

    _scoreArea: [1, 9999999],

    initItem: function (clubId) {
        this._clubId = clubId;
        var clubInfo = ClubDataManager.getClubData(this._clubId);

        for (var i = 0; i < 4; i++){
            this._nodeList['Panel_wanfa.btn' + i].index = i;
        }

        this._nodeList['Panel_search.leftValue'].setString('0');
        this._nodeList['Panel_search.rightValue'].setString('0');

        this._initTableView();
        this._initTimeUnit();
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'dateStartNode.btn_date': '_clickDate',
            'dateEndNode.btn_date': '_clickDate',
            'btn_downday': '_clickDay',
            'btn_upday': '_clickDay',
            'Panel_search.bg_inputLeft': '_clickSearchHandler',
            'Panel_search.btn_edit': '_clickOk',
        };
        for (var i = 0; i < 4; i++){
            this._clickListeners['Panel_wanfa.btn' + i] = '_changeWanfa';
        }

    },
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        this._eventListeners[ClubRecordCode.GetGroupData] = '_groupDataHanlder';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubUserEditOverEvent] = '_clubEditHanlder';
    },

    /**
     * 刷新界面显示
     */
    refreshView: function () {
        // this._nodeList['card_num'].setString(gameData.numOfCards[1] + '张');
        this._changeFilter();
    },
    //主动请求
    _changeFilter: function () {
        //1.请求分组列表QueryClubGroup
        //2.已成功后再请求成员列表allMemberInfo
        //3.讲述组组装成


        //clubMsgManager.requestGroups(this._clubId, true);

        //clubMsgManager.requestClubOwnerFangKa(this._clubId);
        this._queryClubMember();
    },
    /**
     * 请求俱乐部成员数据
     * @private
     */
    _queryClubMember: function(){
        network.send(2103, {cmd: ClubMsgCode.AllMemberInfo, club_id: this._clubId, op: 2});
    },
    _allMemberInfoHandler:function(data) {
        this._memberData = data.info.member;
        var memberData = this._memberData;

        var tmpMemberData;
        var memberLen = memberData.length;
        for (var i = 0; i < memberLen; i++){
            tmpMemberData = memberData[i];
            this.uid2ud[tmpMemberData.u] = tmpMemberData;
        }

        this._queryClubGroupHandler();
    },

    /**
     * 组列表
     * @param data
     * @private
     */
    _queryClubGroupHandler: function(){
        network.send(40001, {
            cmd: ClubRecordCode.GetGroupData,
            area: app.area,
            mapids:  this._curWanfa == '' ? ClubDataManager.getAllAppWanfaMapIdStr() : this._curWanfa,
            clubId: this._clubId,
            startDate: this._nodeList['dateStartNode.dateLabel'].getString().replace(/\//g, '-'),
            endDate: this._nodeList['dateEndNode.dateLabel'].getString().replace(/\//g, '-'),
            orderByType: "winnerCount",
            winScore: this._scoreArea[0],
        });
    },

    //事件回调
    _groupDataHanlder: function (data) {
        //this._tableViewList = data.reverse();
        //去掉未分组
        var groupData = data;
        var singGroupData;
        for (var i = groupData.length - 1; i > -1; i--){
            singGroupData = groupData[i];
            if (singGroupData.group.op_uid == 0){
                groupData.splice(i, 1);
            }
            else {
                singGroupData.headimgurl = this.uid2ud[singGroupData.group.op_uid].h;
            }
        }
        this._tableViewList = groupData;
        this._tableView.reloadData();
    },

    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },
    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubGroup_Hequnshuju_Item', cell, this._tableViewList[idx], this._clubId);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._tableViewList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1016, 106);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },
    /**
     * 初始化时间相关组件
     * @private
     */
    _initTimeUnit:function () {
        var today = new Date();
        this._startYear = this._endYear = today.getFullYear();
        this._startMonth = this._endMonth = today.getMonth() + 1;
        this._startDete = this._endDete = today.getDate();


        this._currentselectYear = this._endYear;
        this._currentselectMonth = this._endMonth;
        this._currentselectday = this._endDete;

        //设置按钮名称 用来区分点击的事那个时间按钮
        this._nodeList['dateStartNode.btn_date'].setName('startDate');
        this._nodeList['dateEndNode.btn_date'].setName('endDate');

        this._refreshDateView(this._nodeList['dateStartNode.dateLabel'], this._startYear, this._startMonth, this._startDete);
        this._refreshDateView(this._nodeList['dateEndNode.dateLabel'], this._endYear, this._endMonth, this._endDete);
    },
    _clickDate: function (target) {
        var today = new Date();
        var curyear = today.getFullYear();
        var curmonth = today.getMonth() + 1;
        var curday = today.getDate();

        var dateLabel = 'dateStartNode.dateLabel';
        if (target.getName() == 'endDate') {
            dateLabel = 'dateEndNode.dateLabel';
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel));
            window.curScene.addChild(_clubPowerDate, 99);
        } else {
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel), null, null, 1);
            window.curScene.addChild(_clubPowerDate, 99);
        }
    },
    /**
     * 时间选中回调结果
     * @private
     */
    _powerDateHandler: function (dateLabel, year, month, day) {
        this._currentSelect = 0;
        this._currentselectYear = year;
        this._currentselectMonth = month;
        this._currentselectday = day;
        this.refrendate(dateLabel, year, month, day);
    },
    refrendate:function (dateLabel, year, month, day) {
        if (dateLabel == 'dateStartNode.dateLabel') {
            this._startYear = year;
            this._startMonth = month;
            this._startDete = day;
        } else {
            this._endYear = year;
            this._endMonth = month;
            this._endDete = day;
        }
        this._refreshDateView(this._nodeList[dateLabel], year, month, day);
        this._queryClubGroupHandler();
    },

    /**
     * 刷新时间显示
     * @private
     */
    _refreshDateView: function (label, year, month, day) {
        if (month < 10) {
            month = '0' + month;
        }
        if (day < 10) {
            day = '0' + day;
        }
        label.setString(year + '/' + month + '/' + day);
    },
    _clickDay:function (target) {
        var btnName = target.getName();
        if (btnName == 'btn_upday') {
            this._currentSelect ++;
        } else {
            this._currentSelect --;
        }
        var selectday = new Date(this._currentselectYear+'/'+this._currentselectMonth+'/'+this._currentselectday+' 00:00:00');
        var jiange = selectday.getTime() - 86400000* this._currentSelect;
        var newdate =  new Date(jiange);
        var maxdate =  new Date();
        var mixdate =  new Date(selectday.getTime() - 86400000* 15);
        if(newdate.getTime() > maxdate.getTime() || newdate.getTime() < mixdate){
            if (btnName == 'btn_upday') {
                this._currentSelect --;
            } else {
                this._currentSelect ++;
            }
            return ;
        }
        var newyear = newdate.getFullYear();
        var newmonth  = newdate.getMonth()+1;
        var newday  = newdate.getDate();
        this.refrendate('dateEndNode.dateLabel',newyear,newmonth,newday);
    },
    _changeWanfa: function(sender){
        var index = sender.index;
        if (this._curWanfa == this._btn2wanfa[index]) return;
        this._curWanfa = this._btn2wanfa[index];

        for (var i = 0; i < 4; i++){
            if (i == index){
                this._nodeList['Panel_wanfa.btn' + i + '.unselect'].setVisible(false);
            }
            else {
                this._nodeList['Panel_wanfa.btn' + i + '.unselect'].setVisible(true);
            }
        }

        this._queryClubGroupHandler();
    },
    _clickSearchHandler: function (target) {
        var clubEditScore = new ClubEditScore(this._editPanelCallBack.bind(this, target.getName()));
        this.addChild(clubEditScore);
        clubEditScore.setPositionX(clubEditScore.getPositionX() - (DesignSize.width - 1016) / 2);
    },
    _editPanelCallBack: function (inputName, str) {
        if (inputName == 'bg_inputLeft') {
            this._nodeList['Panel_search.leftValue'].setString(str);
        } else {
            this._nodeList['Panel_search.rightValue'].setString(str);
        }
    },

    _clickOk: function () {
        var leftValue = parseInt(this._nodeList['Panel_search.leftValue'].getString());
        var rightValue = parseInt(this._nodeList['Panel_search.rightValue'].getString());
        /*
        if (rightValue != 0 && leftValue > rightValue) {
            showAlert('左侧数值不能大于右侧数值!');
            return;
        }
         */
        eventManager.dispatchEvent(ClubUserEditOverEvent, {leftValue: leftValue, rightValue: rightValue});
    },
    _clubEditHanlder: function (data) {
        var leftValue = data['leftValue'] == 0 ? 1 : data['leftValue'];
        //var rightValue = data['rightValue'] == 0 ? 0 : data['rightValue'];
        var rightValue = 9999999;
        this._nodeList['Panel_search.leftValue'].setString(leftValue);
        this._nodeList['Panel_search.rightValue'].setString(rightValue);
        if (leftValue == 0 && rightValue == 0) {
            this._scoreArea = [1, 9999999];
        } else {
            this._scoreArea = [parseInt(leftValue), parseInt(rightValue)];
        }

        this._queryClubGroupHandler();
    },
});